package org.mathig.poker.action;

import org.mathig.poker.PlayerWrapper;
import org.mathig.poker.PlayerListener;
import org.mathig.poker.table.TableHand;

public class RaiseAction extends PlayerAction {

	private int amount;

	public RaiseAction(PlayerWrapper player, TableHand tableHand, int amount) {
		super(player, tableHand);
		this.amount = amount;
	}

	public void execute() {
		// how player add to its previous bet ?
		int oldStack = tableHand.getStack(player);
		int oldBet = tableHand.getBet(player);
		int toAdd = amount - oldBet;

		// raise must be >= 2 * highest bet
		int minRaise = 2 * tableHand.getBet();
		if (amount < minRaise && minRaise <= oldStack) {
			logger.warn("{} raise to {} but less than 2 * highest bet, min raise is {}", new Object[] { player, amount, minRaise });
			toAdd = oldStack;
		}

		toAdd = BetHelper.checkBet(tableHand, player, toAdd);
		BetHelper.addToBet(tableHand, player, toAdd);

		logger.info("{} raise up to {}", player, amount);

		// fire event
		for (PlayerListener listener : tableHand.getPlayerListeners()) {
			if (listener != player)
				listener.playerRaised(player.getPlayer(), tableHand);
		}
	}
}
